package com.lz.springboot.apollo.config;

import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import com.lz.springboot.apollo.Application;

@Component
@EnableApolloConfig({"Java.jdbc"})
@PropertySource(value = "classpath:jdbc.properties", ignoreResourceNotFound = true)
public class ApolloConfigJdbc {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);
	
	@ApolloConfig("Java.jdbc")
	private Config config; 
	
	@Value("${spring.datasource.wlw.url}")
	private String url;
	@Value("${spring.datasource.wlw.username}")
	private String username;
	@Value("${spring.datasource.wlw.password}")
	private String password;
	
	
	@ApolloConfigChangeListener("Java.jdbc")
	private void anotherOnChange(ConfigChangeEvent changeEvent) {
		Set<String> configNames = config.getPropertyNames();
		for(String configName : configNames) {
			LOGGER.info("configName=[{}] configValue=[{}]", configName, config.getProperty(configName, ""));
		}
		
	}

	@Override
	public String toString() {
		return "JdbcConfig [url=" + url + ", username=" + username + ", password=" + password + "]";
	}

	

}